package examples
import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.ml.fpm.{FPGrowth, FPGrowthModel}
import org.apache.spark.sql.{DataFrame, SparkSession}


object FPGrowthExample {

  def main(args: Array[String]): Unit = {

    new SparkContext(new SparkConf().setMaster("local[*]").setAppName(""))

    val spark: SparkSession = SparkSession
      .builder
      .appName(s"${this.getClass.getSimpleName}")
      .getOrCreate()
    import spark.implicits._

    // $example on$
    val dataset: DataFrame = spark.createDataset(Seq(
      "f a c d g i m p",
      "a b c f l m o",
      "b f h j o",
      "b c k s p",
      "a f c e l p m n")
    ).map(t => t.split(" ")).toDF("items")

    val fpgrowth: FPGrowth = new FPGrowth().setItemsCol("items").setMinSupport(0.5).setMinConfidence(0.6)
    val model: FPGrowthModel = fpgrowth.fit(dataset)

    model.freqItemsets.show()

    model.associationRules.show()

    model.transform(dataset).show()

    spark.stop()
  }
}
